package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.commons.constant.SysRetCodeConstants;
import com.cskaoyan.mall.commons.exception.BizException;
import com.cskaoyan.mall.commons.exception.ValidateException;
import com.cskaoyan.mall.commons.util.ObjectEmptyUtils;
import com.cskaoyan.mall.dto.ItemDto;
import com.cskaoyan.mall.dto.ProductDetailDto;
import com.cskaoyan.mall.dto.ProductDto;
import com.cskaoyan.shopping.constants.ShoppingRetCode;
import com.cskaoyan.shopping.converter.ContentConverter;
import com.cskaoyan.shopping.converter.ProductConverter;
import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dal.entitys.ItemDesc;
import com.cskaoyan.shopping.dal.entitys.Panel;
import com.cskaoyan.shopping.dal.entitys.PanelContentItem;
import com.cskaoyan.shopping.dal.persistence.ItemDescMapper;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dal.persistence.PanelMapper;
import com.cskaoyan.shopping.dto.AllProductResponse;
import com.cskaoyan.shopping.dto.PanelContentDto;
import com.cskaoyan.shopping.dto.PanelContentItemDto;
import com.cskaoyan.shopping.dto.PanelDto;
import com.cskaoyan.shopping.form.PageRequest;
import com.cskaoyan.shopping.service.IProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.plaf.synth.SynthOptionPaneUI;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static com.cskaoyan.shopping.constants.GlobalConstants.RECOMMEND_PANEL_ID;

@Service
public class IProductServiceImpl implements IProductService {

    @Autowired
    PanelMapper panelMapper;

    @Autowired
    ContentConverter contentConverter;

    @Autowired
    PanelContentMapper panelContentMapper;

    @Autowired
    ItemMapper itemMapper;

    @Autowired
    ItemDescMapper itemDescMapper;

    @Autowired
    ProductConverter productConverter;

    /**
     * 商品详情
     *
     * @param id
     * @return com.cskaoyan.mall.dto.ProductDetailDto
     * @author 杨宇健
     * @since 2023/01/10 14:09
     */
    @Override
    public ProductDetailDto getProductDetail(Long id) {
        Item item = itemMapper.selectByPrimaryKey(id);
        //判空
        if (item == null || ObjectEmptyUtils.isEmpty(item)) {
            //商品不存在
            throw new BizException(ShoppingRetCode.PRODUCT_NOT_EXISTS.getCode(), ShoppingRetCode.PRODUCT_NOT_EXISTS.getMessage());
        }

        ItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(id);
        if (itemDesc == null || ObjectEmptyUtils.isEmpty(itemDesc)) {
            //商品不存在
            throw new BizException(ShoppingRetCode.PRODUCT_NOT_EXISTS.getCode(), ShoppingRetCode.PRODUCT_NOT_EXISTS.getMessage());
        }
        ProductDetailDto productDetailDto = productConverter.toProductDetailDto(item, itemDesc);
        return productDetailDto;
    }

    @Override
    public AllProductResponse getAllProduct(PageRequest pageInfo) {

        PageHelper.startPage(pageInfo.getPage(), pageInfo.getSize());
        List<Item> items = itemMapper.selectProduct(pageInfo);
        PageInfo<Item> itemPageInfo = new PageInfo<>(items);
        List<ProductDto> productDtos = productConverter.items2Dto(items);
        PageHelper.clearPage();
        return new AllProductResponse(productDtos, itemPageInfo.getTotal());

    }

    @Override
    public Set<PanelDto> getRecommendGoods() {

        //获取推荐内容板块信息
        Panel panel = panelMapper.selectByPrimaryKey(RECOMMEND_PANEL_ID);

        //把DO转化成DTO对象
        Set<PanelDto> panelDtos = new HashSet<>();
        PanelDto panelDto = contentConverter.panel2Dto(panel);
        panelDtos.add(panelDto);

        //根据推荐板块id查询商品id再查询商品信息
        List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(panel.getId());

        //转化成DTO对象
        List<PanelContentItemDto> panelContentItemDtos = contentConverter.panelContentItem2Dto(panelContentItems);

        //封装进panelDto
        panelDto.setPanelContentItems(panelContentItemDtos);

        return panelDtos;

    }

    @Override
    public List<ItemDto> getAllItems() {
        return null;
    }

    @Override
    public Integer getProductStatus(Long id) {

        Item item = itemMapper.selectByPrimaryKey(id);
        int status = item.getStatus();

        return status;
    }
}
